﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace AliCMS.TemplateEngine
{
    internal class Pagination : Tag
    {
        internal Pagination(Engine engine, int index, int length, int contentIndex, int contentLength, Tag parent)
            : base(engine, index, length, contentIndex, contentLength, parent)
        {
        }

        internal override void Init()
        {
            #region FirstPage, CurrentPage, LastPage
            foreach (Tag tag in ChildTags)
            {
                if (tag is FirstPage)
                    _firstPage = tag;
                else if (tag is CurrentPage)
                    _currentPage = tag;
                else if (tag is LastPage)
                    _lastPage = tag;
            }
            #endregion

            #region FirstPageYes, FirstPageNo
            if (_firstPage != null)
            {
                foreach (Tag tag in _firstPage.ChildTags)
                {
                    if (tag is Yes)
                        _firstPageYes = tag;
                    else if (tag is No)
                        _firstPageNo = tag;
                }
            }
            #endregion

            #region CurrentPageYes, CurrentPageNo
            if (_currentPage != null)
            {
                foreach (Tag tag in _currentPage.ChildTags)
                {
                    if (tag is Yes)
                        _currentPageYes = tag;
                    else if (tag is No)
                        _currentPageNo = tag;
                }
            }
            #endregion

            #region LastPageYes, LastPageNo
            if (_lastPage != null)
            {
                foreach (Tag tag in _lastPage.ChildTags)
                {
                    if (tag is Yes)
                        _lastPageYes = tag;
                    else if (tag is No)
                        _lastPageNo = tag;
                }
            }
            #endregion
        }
        internal override void Parse()
        {
            ParseToCode();
            ParseToXml();
        }
        internal override string ParseVariable(Expression variable)
        {
            Match match = Regex.Match(variable.Value, @"^((?<a>FirstPage)|(?<b>PreviousPage)|(?<c>Index)|(?<d>URL)|(?<e>NextPage)|(?<f>LastPage)|(?<g>RecordCount)|(?<h>PageCount)|(?<i>PageSize)|(?<k>RangeLower)|(?<l>RangeUpper))$");
            if (!match.Success)
                return null;

            if (match.Groups["a"].Success)//FirstPage
                return UniqueName + "URL.Replace(\"{Page}\", \"1\")";
            else if (match.Groups["b"].Success)//PreviousPage
                return UniqueName + "URL.Replace(\"{Page}\", Convert.ToString(" + UniqueName + "Page - 1))";
            else if (match.Groups["c"].Success)//Index
                return UniqueName + "Index";
            else if (match.Groups["d"].Success)//URL
                return UniqueName + "URL.Replace(\"{Page}\", Convert.ToString(" + UniqueName + "Index))";
            else if (match.Groups["e"].Success)//NextPage
                return UniqueName + "URL.Replace(\"{Page}\", Convert.ToString(" + UniqueName + "Page + 1))";
            else if (match.Groups["f"].Success)//LastPage
                return UniqueName + "URL.Replace(\"{Page}\", Convert.ToString(" + UniqueName + "PageCount))";
            else if (match.Groups["g"].Success)//RecordCount
                return UniqueName + "RecordCount";
            else if (match.Groups["h"].Success)//PageCount
                return UniqueName + "PageCount";
            else if (match.Groups["i"].Success)//PageSize
                return UniqueName + "PageSize";
            //else if (match.Groups["j"].Success)//PageNumber
            //    return UniqueName + "Page";
            else if (match.Groups["k"].Success)//RangeLower
                return "(" + UniqueName + "Page - 1) * " + UniqueName + "PageSize + 1";
            else if (match.Groups["l"].Success)//RangeUpper
                return UniqueName + "RecordCount < " + UniqueName + "Page * " + UniqueName + "PageSize ? " + UniqueName + "RecordCount : " + UniqueName + "Page * " + UniqueName + "PageSize";
            else
                return null;
        }

        private void ParseToCode()
        {
            Regex re = null;
            Match match = null;

            #region URL
            string url = String.Empty;
            if (Engine.RunAt != "Execute")
            {
                url = (@"
                        private string <%Name%>URL_ = null;
                        private string <%Name%>URL
                        {
                            get
                            {
                                if (<%Name%>URL_ != null)
                                    return <%Name%>URL_;

                                <%Name%>URL_ = BuildUrl(null, null, ""Page={Page}"");
                                return <%Name%>URL_;
                            }
                        }
                        ");
            }
            else
            {
                url = (@"
                        private string <%Name%>URL_ = null;
                        private string <%Name%>URL
                        {
                            get
                            {
                                if (<%Name%>URL_ != null)
                                    return <%Name%>URL_;

                                StringBuilder result = new StringBuilder(""?Page={Page}"");
                                foreach (string key in Request.QueryString.Keys)
                                {
                                    if (key == null || key.ToLower() == ""page"")
                                        continue;

                                    result.Append(""&amp;"" + key + ""="" + Server.UrlEncode(Request.QueryString[key]));
                                }

                                <%Name%>URL_ = result.ToString();
                                return <%Name%>URL_;
                            }
                        }
                        ");
            }
            #endregion

            #region Page
            string page = "1";
            re = new Regex("Page=\"(?<v>.*?)\"", RegexOptions.Compiled);
            match = re.Match(Engine.Text, Index, ContentIndex - Index);
            if (match.Success)
            {
                page = match.Groups["v"].Value;
                if (!Regex.IsMatch(page, @"^\d+$"))
                    page = "Convert.ToInt32(" + Engine.ParseExpression(this, page) + ")";
            }
            #endregion

            #region PageSize
            string pageSize = "20";
            re = new Regex("PageSize=\"(?<v>.*?)\"", RegexOptions.Compiled);
            match = re.Match(Engine.Text, Index, ContentIndex - Index);
            if (match.Success)
            {
                pageSize = match.Groups["v"].Value;
                if (!Regex.IsMatch(pageSize, @"^\d+$"))
                    pageSize = "Convert.ToInt32(" + Engine.ParseExpression(this, pageSize) + ")";
            }
            #endregion

            #region MaxPage
            string maxPage = null;
            re = new Regex("MaxPage=\"(?<v>.*?)\"", RegexOptions.Compiled);
            match = re.Match(Engine.Text, Index, ContentIndex - Index);
            if (match.Success)
            {
                maxPage = match.Groups["v"].Value;
                if (!Regex.IsMatch(maxPage, @"^\d+$"))
                    maxPage = "Convert.ToInt32(" + Engine.ParseExpression(this, maxPage) + ")";
            }
            #endregion

            #region RecordCount
            string recordCount = "0";
            re = new Regex("RecordCount=\"(?<v>.*?)\"", RegexOptions.Compiled);
            match = re.Match(Engine.Text, Index, ContentIndex - Index);
            if (match.Success)
            {
                recordCount = match.Groups["v"].Value;
                if (!Regex.IsMatch(recordCount, @"^\d+$"))
                    recordCount = "Convert.ToInt32(" + Engine.ParseExpression(this, recordCount) + ")";
            }
            #endregion

            #region isAll, currentPageRange
            string isAll = "true";
            string currentPageRange = "0";
            if (_currentPage != null)
            {
                re = new Regex("Range=\"(?<v>.*?)\"", RegexOptions.Compiled);
                match = re.Match(Engine.Text, _currentPage.Index, _currentPage.ContentIndex - _currentPage.Index);
                if (match.Success)
                {
                    currentPageRange = match.Groups["v"].Value;
                    isAll = "false";
                }
            }
            #endregion

            #region FirstPageYesReplacement
            StringBuilder firstPageYesReplacement = new StringBuilder();
            if (_firstPageYes != null)
            {
                foreach (Tag tag in _firstPageYes.ChildTags)
                {
                    if (tag is Code)
                    {
                        firstPageYesReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        firstPageYesReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        firstPageYesReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        firstPageYesReplacement.Append(tag.ResultCode);
                        firstPageYesReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        firstPageYesReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region FirstPageNoReplacement
            StringBuilder firstPageNoReplacement = new StringBuilder();
            if (_firstPageNo != null)
            {
                foreach (Tag tag in _firstPageNo.ChildTags)
                {
                    if (tag is Code)
                    {
                        firstPageNoReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        firstPageNoReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        firstPageNoReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        firstPageNoReplacement.Append(tag.ResultCode);
                        firstPageNoReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        firstPageNoReplacement.Append("firstPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region CurrentPageYesReplacement
            StringBuilder currentPageYesReplacement = new StringBuilder();
            if (_currentPageYes != null)
            {
                foreach (Tag tag in _currentPageYes.ChildTags)
                {
                    if (tag is Code)
                    {
                        currentPageYesReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        currentPageYesReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        currentPageYesReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        currentPageYesReplacement.Append(tag.ResultCode);
                        currentPageYesReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        currentPageYesReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region CurrentPageNoReplacement
            StringBuilder currentPageNoReplacement = new StringBuilder();
            if (_currentPageNo != null)
            {
                foreach (Tag tag in _currentPageNo.ChildTags)
                {
                    if (tag is Code)
                    {
                        currentPageNoReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        currentPageNoReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        currentPageNoReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        currentPageNoReplacement.Append(tag.ResultCode);
                        currentPageNoReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        currentPageNoReplacement.Append("currentPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region LastPageYesReplacement
            StringBuilder lastPageYesReplacement = new StringBuilder();
            if (_lastPageYes != null)
            {
                foreach (Tag tag in _lastPageYes.ChildTags)
                {
                    if (tag is Code)
                    {
                        lastPageYesReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        lastPageYesReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        lastPageYesReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        lastPageYesReplacement.Append(tag.ResultCode);
                        lastPageYesReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        lastPageYesReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region LastPageNoReplacement
            StringBuilder lastPageNoReplacement = new StringBuilder();
            if (_lastPageNo != null)
            {
                foreach (Tag tag in _lastPageNo.ChildTags)
                {
                    if (tag is Code)
                    {
                        lastPageNoReplacement.Append(tag.ResultCode);
                    }
                    else if (tag is OutputStatment)
                    {
                        lastPageNoReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                        lastPageNoReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                    }
                    else if (tag is IfStatment)
                    {
                        lastPageNoReplacement.Append(tag.ResultCode);
                        lastPageNoReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                    }
                    else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                    {
                        lastPageNoReplacement.Append("lastPage.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                    }
                }
            }
            #endregion

            #region AllReplacement
            StringBuilder allReplacement = new StringBuilder();
            foreach (Tag tag in ChildTags)
            {
                if (tag is FirstPage || tag is CurrentPage || tag is LastPage)
                    continue;

                if (tag is Code)
                {
                    allReplacement.Append(tag.ResultCode);
                }
                else if (tag is OutputStatment)
                {
                    allReplacement.Append("object " + tag.UniqueName + " = " + tag.ResultCode + ";\r\n");
                    allReplacement.Append("all.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + " == null ? \"\" : " + tag.UniqueName + ".ToString());\r\n");
                }
                else if (tag is IfStatment)
                {
                    allReplacement.Append(tag.ResultCode);
                    allReplacement.Append("all.Replace(\"{#" + tag.UniqueName + "#}\"," + tag.UniqueName + ");\r\n");
                }
                else if (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+"))
                {
                    allReplacement.Append("all.Replace(\"{#" + tag.UniqueName + "#}\", " + tag.UniqueName + ");\r\n");
                }
            }
            #endregion

            StringBuilder result = new StringBuilder();
            result.Append(@"
                            <%URL%>
                            private int <%Name%>Page;
                            private int <%Name%>PageCount;
                            private int <%Name%>RecordCount;
                            private int <%Name%>PageSize;
                            private int <%Name%>Index;
                            protected string <%Name%>
                            {
                                get
                                {
                                    <%Name%>Page = <%Page%>;
                                    if (<%Name%>Page <= 0)
                                        <%Name%>Page = 1;
                                    <%Name%>PageSize = <%PageSize%>;
                                    <%Name%>RecordCount = <%RecordCount%>;

                                    XmlNode node = _xmlTemplate.SelectSingleNode(""//<%Name%>"");

                                    StringBuilder all = new StringBuilder(node.SelectSingleNode(""All"").InnerText);

                                    <%Name%>PageCount = <%Name%>RecordCount / <%Name%>PageSize;
                                    <%MaxPage%>
                                    if (<%Name%>RecordCount % <%Name%>PageSize > 0)
                                        <%Name%>PageCount += 1;

                                    if (!String.IsNullOrEmpty(Request.QueryString[""GetPageCount""]))
                                        return <%Name%>PageCount.ToString();

                                    //--------------------------------------
                                    //Start : FirstPage
                                    StringBuilder firstPage = new StringBuilder();
                                    string firstPageYesTpl = node.SelectSingleNode(""FirstPage/Yes"").InnerText;
                                    string firstPageNoTpl = node.SelectSingleNode(""FirstPage/No"").InnerText;
                                    if (<%Name%>Page > 1)
                                    {
                                        firstPage.Append(firstPageNoTpl);
                                        <%FirstPageNoReplacement%>
                                    }
                                    else
                                    {
                                        firstPage.Append(firstPageYesTpl);
                                        <%FirstPageYesReplacement%>
                                    }
                                    all.Replace(""{#FirstPage#}"", firstPage.ToString());
                                    //End : FirstPage
                                    //--------------------------------------

                                    //--------------------------------------
                                    //Start : CurrentPage
                                    StringBuilder currentPage = new StringBuilder();

                                    if (<%Name%>PageCount > 1)
                                    {
                                        string currentPageYesTpl = node.SelectSingleNode(""CurrentPage/Yes"").InnerText;
                                        string currentPageNoTpl = node.SelectSingleNode(""CurrentPage/No"").InnerText;

                                        bool isAll = <%IsAll%>;
                                        int pageEnd = 0;
                                        if (isAll)
                                        {
                                            <%Name%>Index = 1;
                                            pageEnd = <%Name%>PageCount;
                                        }
                                        else
                                        {
                                            <%Name%>Index = <%Name%>Page - <%CurrentPageRange%>;
                                            if (<%Name%>Index <= 0)
                                                <%Name%>Index = 1;

                                            pageEnd = <%Name%>Page + <%CurrentPageRange%>;
                                            if (pageEnd > <%Name%>PageCount)
                                                pageEnd = <%Name%>PageCount;
                                        }

                                        while (<%Name%>Index <= pageEnd)
                                        {
                                            if (<%Name%>Index == <%Name%>Page)
                                            {
                                                currentPage.Append(currentPageYesTpl);
                                                <%CurrentPageYesReplacement%>
                                            }
                                            else
                                            {
                                                currentPage.Append(currentPageNoTpl);
                                                <%CurrentPageNoReplacement%>
                                            }
                                            <%Name%>Index++;
                                        }
                                    }
                                    all.Replace(""{#CurrentPage#}"", currentPage.ToString());
                                    //End : CurrentPage
                                    //--------------------------------------

                                    //--------------------------------------
                                    //Start : LastPage
                                    StringBuilder lastPage = new StringBuilder();

                                    string lastPageYesTpl = node.SelectSingleNode(""LastPage/Yes"").InnerText;
                                    string lastPageNoTpl = node.SelectSingleNode(""LastPage/No"").InnerText;

                                    if (<%Name%>Page < <%Name%>PageCount)
                                    {
                                        lastPage.Append(lastPageNoTpl);
                                        <%LastPageNoReplacement%>
                                    }
                                    else
                                    {
                                        lastPage.Append(lastPageYesTpl);
                                        <%LastPageYesReplacement%>
                                    }
                                    all.Replace(""{#LastPage#}"", lastPage.ToString());
                                    //End : LastPage
                                    //--------------------------------------

                                    <%AllReplacement%>

                                    return all.ToString();
                                }
                            }
                          ");
            result.Replace("<%URL%>", url);
            result.Replace("<%Page%>", page);
            result.Replace("<%PageSize%>", pageSize);
            result.Replace("<%RecordCount%>", recordCount);
            if (maxPage != null)
                result.Replace("<%MaxPage%>", UniqueName + "PageCount=" + maxPage + ";");
            else
                result.Replace("<%MaxPage%>", "");
            result.Replace("<%IsAll%>", isAll);
            result.Replace("<%CurrentPageRange%>", currentPageRange);
            result.Replace("<%FirstPageYesReplacement%>", firstPageYesReplacement.ToString());
            result.Replace("<%FirstPageNoReplacement%>", firstPageNoReplacement.ToString());
            result.Replace("<%CurrentPageYesReplacement%>", currentPageYesReplacement.ToString());
            result.Replace("<%CurrentPageNoReplacement%>", currentPageNoReplacement.ToString());
            result.Replace("<%LastPageYesReplacement%>", lastPageYesReplacement.ToString());
            result.Replace("<%LastPageNoReplacement%>", lastPageNoReplacement.ToString());
            result.Replace("<%AllReplacement%>", allReplacement.ToString());
            result.Replace("<%Name%>", UniqueName);
            ResultCode = result.ToString();
        }
        private void ParseToXml()
        {
            #region FirstPageYes
            StringBuilder firstPageYes = new StringBuilder();
            if (_firstPageYes != null)
            {
                firstPageYes = new StringBuilder(Engine.Text.Substring(_firstPageYes.ContentIndex, _firstPageYes.ContentLength));
                for (int i = _firstPageYes.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _firstPageYes.ChildTags[i];
                    firstPageYes.Remove(tag.Index - _firstPageYes.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        firstPageYes.Insert(tag.Index - _firstPageYes.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region FirstPageNo
            StringBuilder firstPageNo = new StringBuilder();
            if (_firstPageNo != null)
            {
                firstPageNo = new StringBuilder(Engine.Text.Substring(_firstPageNo.ContentIndex, _firstPageNo.ContentLength));
                for (int i = _firstPageNo.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _firstPageNo.ChildTags[i];
                    firstPageNo.Remove(tag.Index - _firstPageNo.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        firstPageNo.Insert(tag.Index - _firstPageNo.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region CurrentPageYes
            StringBuilder currentPageYes = new StringBuilder();
            if (_currentPageYes != null)
            {
                currentPageYes = new StringBuilder(Engine.Text.Substring(_currentPageYes.ContentIndex, _currentPageYes.ContentLength));
                for (int i = _currentPageYes.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _currentPageYes.ChildTags[i];
                    currentPageYes.Remove(tag.Index - _currentPageYes.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        currentPageYes.Insert(tag.Index - _currentPageYes.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region CurrentPageNo
            StringBuilder currentPageNo = new StringBuilder();
            if (_currentPageNo != null)
            {
                currentPageNo = new StringBuilder(Engine.Text.Substring(_currentPageNo.ContentIndex, _currentPageNo.ContentLength));
                for (int i = _currentPageNo.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _currentPageNo.ChildTags[i];
                    currentPageNo.Remove(tag.Index - _currentPageNo.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        currentPageNo.Insert(tag.Index - _currentPageNo.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region LastPageYes
            StringBuilder lastPageYes = new StringBuilder();
            if (_lastPageYes != null)
            {
                lastPageYes = new StringBuilder(Engine.Text.Substring(_lastPageYes.ContentIndex, _lastPageYes.ContentLength));
                for (int i = _lastPageYes.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _lastPageYes.ChildTags[i];
                    lastPageYes.Remove(tag.Index - _lastPageYes.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        lastPageYes.Insert(tag.Index - _lastPageYes.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region LastPageNo
            StringBuilder lastPageNo = new StringBuilder();
            if (_lastPageNo != null)
            {
                lastPageNo = new StringBuilder(Engine.Text.Substring(_lastPageNo.ContentIndex, _lastPageNo.ContentLength));
                for (int i = _lastPageNo.ChildTags.Count - 1; i >= 0; i--)
                {
                    Tag tag = _lastPageNo.ChildTags[i];
                    lastPageNo.Remove(tag.Index - _lastPageNo.ContentIndex, tag.Length);
                    if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                        lastPageNo.Insert(tag.Index - _lastPageNo.ContentIndex, "{#" + tag.UniqueName + "#}");
                }
            }
            #endregion

            #region All
            StringBuilder all = new StringBuilder(Engine.Text.Substring(ContentIndex, ContentLength));
            for (int i = ChildTags.Count - 1; i >= 0; i--)
            {
                Tag tag = ChildTags[i];
                all.Remove(tag.Index - ContentIndex, tag.Length);
                if (tag is IfStatment || tag is OutputStatment || (tag.ResultCode != null && Regex.IsMatch(tag.ResultCode, "protected string " + tag.UniqueName + @"\s+{\s+get\s+")))
                {
                    all.Insert(tag.Index - ContentIndex, "{#" + tag.UniqueName + "#}");
                }
                else
                {
                    if (tag is FirstPage)
                        all.Insert(tag.Index - ContentIndex, "{#FirstPage#}");
                    else if (tag is CurrentPage)
                        all.Insert(tag.Index - ContentIndex, "{#CurrentPage#}");
                    else if (tag is LastPage)
                        all.Insert(tag.Index - ContentIndex, "{#LastPage#}");
                }
            }
            #endregion

            #region 生成Xml
            StringBuilder result = new StringBuilder();
            result.Append(@"
		                <FirstPage>
			                <Yes>
				                <![CDATA[
				                <%FirstPageYes%>
				                ]]>
			                </Yes>
			                <No>
				                <![CDATA[
				                <%FirstPageNo%>
				                ]]>
			                </No>
		                </FirstPage>
		                <CurrentPage>
			                <Yes>
				                <![CDATA[
				                <%CurrentPageYes%>
				                ]]>
			                </Yes>
			                <No>
				                <![CDATA[
				                <%CurrentPageNo%>
				                ]]>
			                </No>
		                </CurrentPage>
		                <LastPage>
			                <Yes>
				                <![CDATA[
				                <%LastPageYes%>
				                ]]>
			                </Yes>
			                <No>
				                <![CDATA[
				                <%LastPageNo%>
				                ]]>
			                </No>
		                </LastPage>
		                <All>
			                <![CDATA[
			                <%All%>
			                ]]>
		                </All>
                        ");
            result.Replace("<%FirstPageYes%>", firstPageYes.ToString());
            result.Replace("<%FirstPageNo%>", firstPageNo.ToString());
            result.Replace("<%CurrentPageYes%>", currentPageYes.ToString());
            result.Replace("<%CurrentPageNo%>", currentPageNo.ToString());
            result.Replace("<%LastPageYes%>", lastPageYes.ToString());
            result.Replace("<%LastPageNo%>", lastPageNo.ToString());
            result.Replace("<%All%>", all.ToString());
            ResultXml = result.ToString();
            #endregion
        }

        #region Fields
        private Tag _firstPage;
        private Tag _firstPageYes;
        private Tag _firstPageNo;
        private Tag _currentPage;
        private Tag _currentPageYes;
        private Tag _currentPageNo;
        private Tag _lastPage;
        private Tag _lastPageYes;
        private Tag _lastPageNo;
        #endregion
    }
}
